package com.hmz.backend.service.Impl.account;

import cn.hutool.json.JSONObject;
import com.hmz.backend.entity.User;
import com.hmz.backend.mapper.UserMapper;
import com.hmz.backend.service.account.RegisterService;
import com.hmz.backend.utils.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("registerUserService")
public class RegisterUserServiceImpl implements RegisterService {

    @Autowired
    UserMapper userMapper;
    @Override
    public JSONObject register(JSONObject data) {
        User user = new User();
        user.setUserName((String) data.get("userName"));
        user.setPassword(PasswordEncoder.encode(data.get("password").toString()));
        user.setUserType(Integer.parseInt(data.get("userType").toString()));
        if(user.getUserType() == 1 && !data.get("adminKey").toString().equals("admin" ) ) return  new JSONObject().set("message","注册失败,请联系管理人员，管理员密钥错误");
        try{
            int result =  userMapper.insert(user) ;

        }catch (Exception e) {
            System.out.println(e);
            return new JSONObject().set("message","注册失败,用户名已经被人注册了！");
        }
        return  new JSONObject().set("message","注册成功！");
    }
}
